package org.example.controller;

import org.example.pojo.Product;
import org.example.pojo.ProductTag;
import org.example.pojo.Result;
import org.example.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/product")
public class ProductController {

    private final ProductServiceImpl productService;

    @Autowired
    public ProductController(ProductServiceImpl productService) {
        this.productService = productService;
    }

    /**
     * 获取所有商品列
     */
    @GetMapping
    public Result getAllProducts() {
        List<Product> products = productService.findAll();
        return Result.success(products);
    }

    /**
     * 删除指定商品
     */
    @DeleteMapping("/{id}")
    public Result deleteProduct(@PathVariable Long id) {
        productService.deleteById(id);
        return Result.success();
    }

    /**
     * 获取指定商品的标签列表
     */
    @GetMapping("/{id}/tags")
    public Result getProductTags(@PathVariable Long id) {
        List<ProductTag> tags = productService.findTagsByProductId(id);
        return Result.success(tags);
    }

    /**
     * 为商品添加新标签
     */
    @PostMapping("/{id}/tags")
    public Result addTagToProduct(
            @PathVariable Long id,
            @RequestBody ProductTag tag) {
        // 设置商品ID关联关系
        tag.setProductId(id);
        ProductTag savedTag = productService.addTag(tag);
        return Result.success(savedTag);
    }
}